<?php
/**
 * 影院管理 -> 添加影院专员
 */

class Action
{
    /**
     * 添加影院专员
     */
    public function add()
    {
        $id = Request::Get('id', 0);

        $user_area_list = array();
        if ($id) {
            $user_info          = _uri('user', $id);
            $director_user_info = _model('user_cinema_assistant')->read(array('user_id'=>$id));

            if ($director_user_info) {
                $user_info['director_id'] = $director_user_info['director_id'];
            }

            $user_area = _model('multiple_area')->getList(array('user_id' => $id));
            foreach($user_area as $v){
                $user_area_list[] = $v['province_code'];
            }

            Response::assign('info', $user_info);
        }

        $area = _model('region')->getList(array('depth' => 1, 'status' => 1));

        $director_list = user_helper::get_director_list();
        if ($director_list) {
            $director_list = array_to_option($director_list,'realname');
            Response::assign('director_list', $director_list);
        }

        Response::assign('area', $area);
        Response::assign('user_area', $user_area_list);
        Response::assign('role', user_config::$role_arr[user_config::CINEMA_ASSISTANT]);
        Response::assign('role_id', user_config::CINEMA_ASSISTANT);
        Response::display('admin/add_cinema_assistant.html');
    }

    /**
     * 保存影院
     */
    public function save()
    {
        $id          = Request::getParam('id', 0);
        $info        = Request::getParam('info', array());
        $hash        = user_helper::random_hash(6);
        $area        = Request::getParam('area', array());

        $role_id     = $info['role_id'];
        $password    = $info['password'];
        $director_id = $info['director_id'];

        // 检查用户手机号和邮箱
        $result = user_helper::common_check_save($info);
        if ($result[0] !== true) {
            return $result;
        } else {
            $info = $result[1];
        }

        unset($info['director_id']);
        if ($id) {
            $result = user_helper::common_update_user_info($info, $password, $hash, $id);
            if ($result[0] === true) {
                _model('user_cinema_assistant')->update(array('user_id' => $id), array('director_id'=>$director_id));
            } else {
                return $result;
            }
            _model('multiple_area')->delete(array('user_id' => $id));
            $user_id = $id;
        } else {

            $result = user_helper::common_create_user_info($info, $password, $role_id, $hash, $director_id);
            if ($result[0] === true) {
                $user_id = $result[1];
                _model('user_cinema_assistant')->create(array('user_id' => $user_id, 'director_id'=>$director_id));
            } else {
                return $result;
            }
        }

        foreach($area as $item){
            _model('multiple_area')->create(array('user_id' => $user_id, 'province_code' => $item, 'add_time' => datetime(), 'status' => 1));
        }

        Response::redirect(AnUrl('user/admin'));
    }
}
?>